package com.gm.commonlib.base.baseui.baseactivity;

import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.FragmentActivity;

import com.gm.commonlib.application.CommonBaseApplication;
import com.gm.commonlib.base.basePresenter.BasePresenter;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.manger.ActivityMangers;
import com.gm.commonlib.module.evenbus.EventBusUtils;
import com.gm.commonlib.module.evenbus.EventMessage;
import com.gm.commonlib.views.dialog.loadingDialog.ILoadingDialog;
import com.gm.commonlib.views.toast.IToast;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public abstract class CommonAbstractBaseActivity extends FragmentActivity implements  IBaseView {

    protected CommonBaseApplication app;
    protected CommonAbstractBaseActivity mThisActivity;
    protected Context context;

    protected ILoadingDialog mILoadingDialog; //加载提示框
    protected IToast mToast; //toast

    protected BasePresenter mPresenter;
    abstract ILoadingDialog createLoadingDialog();
    abstract IToast createToast();

    abstract BasePresenter createPresenter();
    protected void installAppContext(Application application) {
        if (application instanceof CommonBaseApplication){
            app = (CommonBaseApplication) application;
        }

    }

    public CommonBaseApplication getBaseApplication() {
        return app;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();
        //把activity加入集合中管理
        ActivityMangers.getActivityMangers().addActivity(this);
        if (mPresenter!=null){
            mPresenter.onAttach(this);
        }
    }


    //初始化
    protected void init(){
        mPresenter=createPresenter();
        context = this;
        mThisActivity = this;
        installAppContext(getApplication());
        //加载框初始化
        mILoadingDialog=createLoadingDialog();
        mToast=createToast();
    }

    @Override
    public void onStart() {
        super.onStart();
        //todo 每个继承这个基类的类 启动就注册上evenbus事件监听
        EventBusUtils.register(this);
    }


    @Override
    protected void onDestroy() {
        EventBusUtils.unregister(this);
        ActivityMangers.getActivityMangers().removeActivity(this);
        hidLoadingDialog();
        if (mPresenter!=null){
            mPresenter.onDetach();
        }
        super.onDestroy();
    }



    public Context getContext() {
        return context;
    }


    public Dialog showLoadingDialog() {
        return showLoadingDialog(null);
    }

    public Dialog showLoadingDialog(String msg) {
        if (mILoadingDialog!=null){
            return mILoadingDialog.show(context,msg,false);
        }
        return null;
    }

    public Dialog showLoadingDialog(String msg,boolean cancelable) {
        if (mILoadingDialog!=null){
            return mILoadingDialog.show(context,msg,cancelable);
        }
        return null;
    }


    public void hidLoadingDialog() {
        if (mILoadingDialog!=null){
            mILoadingDialog.hid();
        }

    }


    protected CommonAbstractBaseActivity getActivity() {
        return this;
    }


    public void showToast(String text) {
       if (mToast!=null){
           mToast.showToast(text);
       }
    }


    public int getColorById(int colorId){
        return  getResources().getColor(colorId);
    }


    /**
     * 接收到分发的事件
     *
     * @param event 事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveEvent(EventMessage event) {
    }

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onReceiveStickyEvent(EventMessage event) {
    }

}
